package com.gite.nouette.admin.implementation.view;

import com.gite.nouette.admin.contract.controller.ICManager;
import com.gite.nouette.admin.contract.view.IVContainerModule;
import com.gite.nouette.admin.contract.view.IVHandlerModule;
import com.gite.nouette.admin.contract.view.IVManager;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;

public class VManagerImpl extends Tab implements IVManager {
	private ICManager c;
	private final static int POSITION_CONTAINER_MODULE = 0;
	private final static int POSITION_HANDLER_MODULE = 1;

	public VManagerImpl() {
		init();
	}

	@Override
	public String getName() {
		return getTitle();
	}

	@Override
	public void setName(String name) {
		setTitle(name);
	}

	public void init() {
		setMainLayout(new HLayout());
	}

	@Override
	public ICManager getC() {
		return this.c;
	}

	@Override
	public void setC(ICManager controller) {
		this.c = controller;
	}

	@Override
	public String getTitre() {
		return getTitle();
	}

	@Override
	public void setTitre(String titre) {
		setTitle(titre);
	}

	private HLayout getMainLayout() {
		return (HLayout) getPane();
	}

	private void setMainLayout(HLayout mainLayout) {
		setPane(mainLayout);
	}

	@Override
	public IVHandlerModule getHandlerModule() {
		return (IVHandlerModule) getMainLayout().getMember(
				POSITION_HANDLER_MODULE);
	}

	@Override
	public void setHandlerModule(IVHandlerModule handlerModule) {
		getMainLayout().addMember((Widget) handlerModule,
				POSITION_HANDLER_MODULE);
	}

	@Override
	public IVContainerModule getContainerModule() {
		return (IVContainerModule) getMainLayout().getMember(
				POSITION_CONTAINER_MODULE);
	}

	@Override
	public void setContainerModule(IVContainerModule containerModule) {
		removeContainerModule();
		getMainLayout().addMember((Widget) containerModule,
				POSITION_CONTAINER_MODULE);
	}

	public boolean removeContainerModule() {
		IVContainerModule containerModule = getContainerModule();
		return removeCanvas((Canvas) containerModule);
	}

	public boolean removeHandlerModule() {
		IVHandlerModule handlerModule = getHandlerModule();
		return removeCanvas((Canvas) handlerModule);
	}

	private boolean removeCanvas(Canvas canvas) {
		if (canvas != null) {
			getMainLayout().removeMember(canvas);
			return true;
		}
		return false;
	}
}
